﻿using System;
using Rabbit.Logic;
using RabbitShop.Code;

namespace RabbitShop.Action
{
    public partial class AddressDelete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }

            string aid = Request["aid"];
            if (string.IsNullOrEmpty(aid)) return;
            int addressId;
            if (int.TryParse(aid, out addressId))
            {
                Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
                customer.AddressList.RemoveAt(addressId);
                for (int i = 0; i < customer.AddressList.Count; i++)
                {
                    customer.AddressList[i].Id = i.ToString();
                }
                RabbitUse.AddOrUpdateCustomer(Db.Store, customer);
            }
            
            Response.Redirect("../UI/UserAddress.aspx");
        }
    }
}